using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{

   public class CLSREPPRO : ObservableObject
   {

       MDOgeneral mod = new MDOgeneral();

       public DataTable consProOrden() 
       {
            try {
                string query = "select a.codprc_rpp, b.nomprc_ppr, ''||a.fecrep_rpp||'' as fecrep_rpp, a.codemp_rpp, c.nomemp_emp, a.nroord_rpp, a.canrep_rpp  from reppro a inner join prcprd b on a.codprc_rpp=b.codprc_ppr inner join (select codemp_emp, nomemp_emp from maeempp union select distinct codequ_equ as codemp_emp, ''||codequ_equ||'' as nomemp_emp from maeequ) c on a.codemp_rpp=c.codemp_emp where a.nroord_rpp='" + NROORD_RPP + "' order by a.codprc_rpp, a.fecrep_rpp";
            return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
       } 

       //Consultar cantidad reportada para una orden en un proceso
       public void consCanRep() {
           try {
               string query = "select coalesce(sum(canrep_rpp),0) as canrep_rpp  from reppro where codprc_rpp='" + CODPRC_RPP + "' and nroord_rpp='" + NROORD_RPP + "'";
               DataTable val = mod.consulta(query);

               if (val.Rows.Count > 0)
               {

                   CANREP_RPP =Convert.ToDouble(val.Rows[0]["canrep_rpp"]);
               }
               else {

                   CANREP_RPP = 0;               
               }          
           }
           catch (Exception ex) {throw ex; }
       }
       
       //Consulta cantidad reportada para todos los procesos
       public List<CLSREPPRO> consCanRepPro()
       {
           try {

               string query = "select  codprc_rpp,sum(canrep_rpp) as canrep_rpp from reppro where nroord_rpp='"+NROORD_RPP+"' group by codprc_rpp";
               DataTable val = mod.consulta(query);
               List<CLSREPPRO> lisRepPro = new List<CLSREPPRO>();
               foreach (DataRow r in val.Rows) {
                   lisRepPro.Add(new CLSREPPRO {
                       CODPRC_RPP = r["codprc_rpp"].ToString(),
                       CANREP_RPP =Convert.ToDouble(r["canrep_rpp"])
                   }   
                 );
               }
               return lisRepPro;
           }
           catch (Exception ex) { throw ex; }
       }

       public int guaCorte(List<CLSREPPRO>  lrep) {
           try {

               MDOgeneral.transaccion = MDOgeneral.conexion.BeginTransaction();
               foreach (CLSREPPRO r in lrep) {
                    r.insertar();
               }
               MDOgeneral.transaccion.Commit();
               return 1;
           }
           catch (Exception ex) {
               MDOgeneral.transaccion.Rollback();
               throw ex;
           }
       }

       public void conCons() {
           try {
               string query = "select coalesce(max(consec_rpp),0)+1 as consec_rpp from reppro where ano_rpp="+ANO_RPP+" and sem_rpp="+SEM_RPP+" and codprc_rpp='"+CODPRC_RPP+"' and nroord_rpp='"+NROORD_RPP+"'";
               DataTable val=mod.consulta(query);
               if (val.Rows.Count > 0) {
                   CONSEC_RPP = Convert.ToInt32(val.Rows[0]["consec_rpp"]);
               }else{
                  CONSEC_RPP=0;    
               }
           }
           catch (Exception ex) {throw ex;}
       }

       public int insertar() {
           try {
               conCons();
               string query = "INSERT INTO REPPRO (ANO_RPP, SEM_RPP, CODPRC_RPP, NROORD_RPP, CONSEC_RPP, FECREP_RPP, CODEMP_RPP, CANREP_RPP, PORCEN_RPP, VLRCTG_RPP, VLRVLR_RPP, ESTADO_RPP) VALUES ('" + ANO_RPP + "', '" + SEM_RPP + "', '" + CODPRC_RPP + "', '" + NROORD_RPP + "', '" + CONSEC_RPP + "', '"+FECREP_RPP.ToShortDateString()+"', '"+CODEMP_RPP+"', '"+CANREP_RPP+"', '"+PORCEN_RPP+"', '"+VLRCTG_RPP+"', '"+VLRVLR_RPP+"', '"+ESTADO_RPP+"')";
               return mod.comando(query);
           }
           catch (Exception ex) { throw ex; }
       }

       private string _NROORD_RPP;
       public string NROORD_RPP 
       {
           get { return _NROORD_RPP; }
           set { _NROORD_RPP = value; RaisePropertyChangedEvent("NROORD_RPP"); }
       }

       private string _CODPRC_RPP;
       public string CODPRC_RPP 
       {
           get { return _CODPRC_RPP; }
           set { _CODPRC_RPP = value; RaisePropertyChangedEvent("CODPRC_RPP"); }
       }

       private double _CANREP_RPP;
       public double CANREP_RPP 
       {
           get { return _CANREP_RPP; }
           set { _CANREP_RPP = value; RaisePropertyChangedEvent("CANREP_RPP"); }
       }

       private int _ANO_RPP;
       public int ANO_RPP 
       {
            get { return _ANO_RPP; }
            set { _ANO_RPP = value; RaisePropertyChangedEvent("ANO_RPP"); }
        }

       private int _SEM_RPP;
       public int SEM_RPP
       {
            get { return _SEM_RPP; }
            set { _SEM_RPP = value; RaisePropertyChangedEvent("SEM_RPP"); }
       }

       private int _CONSEC_RPP;
       public int CONSEC_RPP
       {
            get { return _CONSEC_RPP; }
            set { _CONSEC_RPP = value; RaisePropertyChangedEvent("CONSEC_RPP"); }
       }

       private DateTime _FECREP_RPP;
       public DateTime  FECREP_RPP
       {
            get { return _FECREP_RPP; }
            set { _FECREP_RPP = value; RaisePropertyChangedEvent("FECREP_RPP"); }
       }

       private string _CODEMP_RPP;
       public string CODEMP_RPP
       {
            get { return _CODEMP_RPP; }
            set { _CODEMP_RPP = value; RaisePropertyChangedEvent("CODEMP_RPP"); }
       }

       private double _PORCEN_RPP;
       public double PORCEN_RPP
       {
            get { return _PORCEN_RPP; }
            set { _PORCEN_RPP = value; RaisePropertyChangedEvent("PORCEN_RPP"); }
       }

       private double _VLRCTG_RPP;
       public double VLRCTG_RPP
       {
            get { return _VLRCTG_RPP; }
            set { _VLRCTG_RPP = value; RaisePropertyChangedEvent("VLRCTG_RPP"); }
       }

       private double _VLRVLR_RPP;
       public double VLRVLR_RPP
       {
            get { return _VLRVLR_RPP; }
            set { _VLRVLR_RPP = value; RaisePropertyChangedEvent("VLRVLR_RPP"); }
       }

       private string _ESTADO_RPP;
       public string ESTADO_RPP
       {
            get { return _ESTADO_RPP; }
            set { _ESTADO_RPP = value; RaisePropertyChangedEvent("ESTADO_RPP"); }
       }

       private Int32 _numConsecutivo;
       public Int32 numConsecutivo
       {
           get { return _numConsecutivo; }
           set { _numConsecutivo = value; RaisePropertyChangedEvent("numConsecutivo"); }
       }

       private double _numPendiente;
       public double numPendiente
       {
           get { return _numPendiente; }
           set { _numPendiente = value; RaisePropertyChangedEvent("numPendiente"); }
       }

       public CLSMAEREF referencia = new CLSMAEREF();
       public CLSMAECOL color = new CLSMAECOL();
       public CLSDETORDP detalleOrdenes = new CLSDETORDP();
   }
}
